﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Groups : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Id { get; set; }
        public List<Group> GroupList { get; set; }

        public Groups()
        {}

        public Groups(XmlNode node):this()
        {
            try
            {
                this.Id = (string)XmlHelper.ExtractXmlAttributeToProperty2(node, "Id", XmlHelper.PropertyType.stringType);

                this.GroupList = new List<Group>();

                foreach (XmlNode groupNode in node.SelectNodes("Group"))
                {
                    if (groupNode.NodeType == XmlNodeType.Comment)
                        return;

                    Group grp = new Group(groupNode);
                    this.GroupList.Add(grp);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode groupsNode = XmlHelper.AddNode(commandUINode, "Groups");
                XmlHelper.AddAttribute(groupsNode, "Id", this.Id);

                foreach (Group grp in this.GroupList)
                {
                    grp.AddNode(groupsNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Groups");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Group" }, true);

                if (this.GroupList != null)
                {
                    foreach (Group grp in this.GroupList)
                    {
                        grp.BuildTreeViewElement(node);
                    }
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Group))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.GroupList == null)
                    {
                        this.GroupList = new List<Group>();
                    }

                    this.GroupList.Add((Group)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Group)
                {
                    this.GroupList.Remove((Group)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
